<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" />
        <!-- 树形组件 -->
        <el-tree ref="deptTree" node-key="id" :data="depts" :props="defaultProps" default-expand-add :expand-on-click-node="false" highlight-current @current-change="selectNode" />
      </div>

      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table v-loading="loading" :data="list">
          <el-table-column label="头像" width="80px" prop="staffPhoto">
            <template v-slot="{row}">
              <img v-if="row.staffPhoto" :src="row.staffPhoto" class="avatar" alt="">
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作">
            <template>
              <el-button type="text" size="mini">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-button type="text" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <template>
          <div class="block">
            <span class="demonstration" />
            <el-pagination
              layout="prev, pager, next"
              :total="total"
              :current-page="queryParams.page"
              :page-size="queryParams.pagesize"
              @current-change="vhangePage"
            />
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import { EmployeeAPI, getDepartMentAPI } from '@/api'

import { ListTotreeData } from '@/utils'

export default {
  name: 'Employee',
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryParams: {
        page: 1,
        pagesize: 10,
        departmentId: null
      },
      list: [],
      loading: false,
      total: 0
    }
  },
  created() {
    this.getDepartMent()
  },
  methods: {
    async getDepartMent() {
      this.depts = ListTotreeData(await getDepartMentAPI(), 0)
      this.queryParams.departmentId = this.depts[0].id
      this.getEmployee()
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentNode(this.depts[0])
      })
    },
    // setDepartMent(node) {
    //   this.queryParams.departmentId = node.id
    //   // this.getEmployee()
    // },
    selectNode(data) {
      this.queryParams.departmentId = data.id
      this.queryParams.page = 1
      this.getEmployee()
    },
    async getEmployee() {
      this.loading = true
      const { rows, total } = await EmployeeAPI(this.queryParams)
      this.list = rows
      this.loading = false
      this.total = total
    },
    vhangePage(newpage) {
      this.queryParams.page = newpage
      this.getEmployees()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
    .avatar{
      width: 30px;
      height: 30px;
    }
  }
}
.block{
  display: flex;
  justify-content: end;
  margin-top: 10px;
}
</style>
